home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / resources / templates / osxdnd.js < prev    next >
Encoding:
JavaScript  |  2007-11-12  |  4.3 KB  |  132 lines

  1. /*
  2. # Miro - an RSS based video player application
  3. # Copyright (C) 2005-2007 Participatory Culture Foundation
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  18. */
  19.  
  20. /*
  21.  * osxdnd.js
  22.  * DND support for OS X.
  23.  * NOTE: This file gets included for all the platforms, but it's only used in OSX.  OSX hooks up these functions to the various 
  24.  * drag and drop events using HTMLArea.getBodyTagExtra()
  25.  */
  26.  
  27. var dragHighlight = {
  28.   highlightedElement: null,
  29.   highlightCSSClass: null,
  30.  
  31.   removeHighlight: function() {
  32.     if(this.highlightedElement) {
  33.       this.highlightedElement.className = 
  34.         this.highlightedElement.className.replace(this.highlightCSSClass, "");
  35.       this.highlightedElement = null;
  36.     }
  37.   },
  38.  
  39.   setHightlight: function(element, type) {
  40.     this.removeHighlight();
  41.     if(element.className) {
  42.       this.highlightCSSClass = " drag-highlight " + type;
  43.     } else {
  44.       this.highlightCSSClass = "drag-highlight " + type;
  45.     }
  46.     element.className += this.highlightCSSClass;
  47.     this.highlightedElement = element;
  48.   }
  49. };
  50.  
  51. function canElementSupportDrag(element, dataTransfer) {
  52.   var dragDestTypes = element.getAttribute("dragdesttype").split(":");
  53.   for(var i = 0; i < dragDestTypes.length; i++) {
  54.     var dragDestType = dragDestTypes[i];
  55.     var mimeType = "application/x-democracy-" + dragDestType + "-drag";
  56.     for(var j = 0; j < dataTransfer.types.length; j++) {
  57.       if(dataTransfer.types[j] == mimeType) return dragDestType;
  58.     }
  59.   }
  60.   return false;
  61. }
  62.  
  63. function searchUpForElementWithAttribute(element, attributeName) {
  64.     while (1) {
  65.     if (element.nodeType == 1 && element.getAttribute(attributeName)) {
  66.             return element;
  67.     }
  68.     if (element.parentNode) {
  69.         element = element.parentNode;
  70.         } else {
  71.             return null;
  72.         }
  73.     }
  74.  
  75.     return null;
  76. }
  77.  
  78. function findDropInfo(startElement) {
  79.   var elt = searchUpForElementWithAttribute(event.target, "dragdesttype");
  80.   while(elt) {
  81.     var dragDestType = canElementSupportDrag(elt, event.dataTransfer);
  82.     if(dragDestType) {
  83.         return {'dragDestType': dragDestType, 'element': elt};
  84.     } 
  85.     elt = searchUpForElementWithAttribute(elt.parentNode, "dragdesttype");
  86.   }
  87.   return null;
  88. }
  89.  
  90.  
  91. function handleDragStart(event) {
  92.    var elt = searchUpForElementWithAttribute(event.target, "dragsourcetype");
  93.    if(elt) {
  94.       var dragSourceType = elt.getAttribute("dragsourcetype") 
  95.       var mimeType = "application/x-democracy-" + dragSourceType + "-drag";
  96.       event.dataTransfer.setData(mimeType, elt.getAttribute("dragsourcedata"));
  97.       event.dataTransfer.effectAllowed = "all";
  98.       var dragImage = document.getElementById(elt.getAttribute('dragicon'));
  99.       event.dataTransfer.setDragImage(dragImage, 5, 5);
  100.       return false;
  101.    }
  102. }
  103.  
  104. function handleDragOver(event) {
  105.   var dropInfo = findDropInfo(event.target);
  106.   if(dropInfo) {
  107.     var dragDestType = dropInfo['dragDestType'];
  108.     var elt = dropInfo['element'];
  109.     event.dataTransfer.dropEffect = elt.getAttribute('drageffect' + dragDestType);
  110.     event.preventDefault();
  111.     dragHighlight.setHightlight(elt, dragDestType);
  112.   } 
  113. }
  114.  
  115. function handleDragLeave(event) {
  116.   dragHighlight.removeHighlight();
  117. }
  118.  
  119. function handleDrop(event) {
  120.   dragHighlight.removeHighlight();
  121.   var dropInfo = findDropInfo(event.target);
  122.   if(dropInfo) {
  123.     var dragDestType = dropInfo['dragDestType'];
  124.     var elt = dropInfo['element'];
  125.     var dragDestMimeType = "application/x-democracy-" + dragDestType + "-drag";
  126.     var sourceData = event.dataTransfer.getData(dragDestMimeType);
  127.       var dragDestData = elt.getAttribute("dragdestdata");
  128.       eventURL('action:handleDrop?data=' + dragDestData + "&type=" +
  129.           dragDestType + "&sourcedata=" + sourceData);
  130.   }
  131. }
  132.